package com.cms.web.servlet;

import com.cms.pojo.Course;
import com.cms.pojo.Student;
import com.cms.pojo.Teacher;
import com.cms.service.CourseService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/addCourseServlet")
public class AddCourseServlet extends HttpServlet {
    private CourseService courseService = new CourseService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取用户输入
        String name = request.getParameter("name");
        String t_name = request.getParameter("t_name");
        int total_num = Integer.parseInt(request.getParameter("total_num"));
        double credit = Double.parseDouble(request.getParameter("credit"));
        double total_hours = Double.parseDouble(request.getParameter("total_hours"));
        int role = Integer.parseInt(request.getParameter("role"));

        // 调用service查老师id
        int id = courseService.selectIdByName(t_name);

        Course course = new Course();
        course.setName(name);
        course.setT_name(t_name);
        course.setT_id(id);
        course.setTotalnum(total_num);
        course.setCredit(credit);
        course.setClass_hours(total_hours);
        course.setSort(role);

        // 调用service添加
        courseService.addCourse(course);

        request.getRequestDispatcher("/allCourseServlet").forward(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
